﻿using BLL;
using HRP.Utilidades;
using Model;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HRP.Private.Impressoes
{
    public partial class Vencimento : System.Web.UI.Page
    {
        private string font = "Verdana";
        private string TituloLabel = "Recibo de Vencimento";
        private string NomeFuncLabel = "Nome";
        private string NumeroFuncLabel = "Número";
        private string NumContribuinteFuncLabel = "NIF";
        private string DepartamentoLabel = "Sector";
        private string CargoLabel = "Categoria";
        private string AbonoLabel = "Abono";
        private string CoefLabel = "Coef.";
        private string ValorLabel = "Valor";
        private string DescontoLabel = "Desconto";
        private string TotalAbonosLabel = "Abonos";
        private string TotalDescontosLabel = "Descontos";
        private string LiquidoReceberLabel = "Líquido a Receber";
        private string DataLabel = "Data";
        private string AssinaturaLabel = "Assinatura";
        private string DeclaracaoLabel = "(Declaro ter recebido desta Empresa todas as renumerações a que tenho direito até esta data)";
        private string NumContribuinteLabel = "Nº Contribuinte: ";
        private string NomeEmpresaLabel = "Entidade: ";
        private string autor = "HR Portal";
        private string assunto = "Recibos de Vencimento";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null && Session["EmpresaActual"]!=null)
            {
                //todo: validar se a factura pertence ao cliente e empresa que esta logado
                ImprimirListaVencimentos();
            }

        }

        private string Escreve(string valor, string comparacao, string valorOmissao)
        {
            return valor == comparacao ? valorOmissao : valor;
        }

        private List<LinhaVencimento> ObterAbonos(hrp_facturacao_funcionario_detalhe funcionario)
        {

            List<LinhaVencimento> ladoEsq = new List<LinhaVencimento>();
            ladoEsq.Add(new LinhaVencimento() { Abono = "Salário Base", Coef = Escreve(funcionario.total_dias_facturados.ToString(), "30", ""), Valor = funcionario.salarioBruto.ToString() });
            ladoEsq.Add(new LinhaVencimento() { Abono = "Sub", Coef = "", Valor = "" });
            ladoEsq.Add(new LinhaVencimento() { Abono = "10%", Coef = "", Valor = Escreve(funcionario.sub10.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "5% Exposição", Coef = "", Valor = Escreve(funcionario.subs_exposicao.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "5% Contágio", Coef = "", Valor = Escreve(funcionario.subs_contagio.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "3% Compensação", Coef = "", Valor = Escreve(funcionario.subs_compensacao.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "Chefia", Coef = "", Valor = Escreve(funcionario.subs_chefia.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "Abono de Família", Coef = "", Valor = Escreve(funcionario.subs_abonofamilia.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "Abono", Coef = "", Valor = Escreve(funcionario.subs_abono.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "Subsídio de Turno", Coef = "", Valor = Escreve(funcionario.subs_turno.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "Sub. Transporte", Coef = "", Valor = Escreve(funcionario.subs_transporte.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "Sub. Assiduidade", Coef = "", Valor = Escreve(funcionario.subs_assiduidade.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "Sub. Desempenho", Coef = "", Valor = Escreve(funcionario.subs_desempenho.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "Sub. Produtividade", Coef = "", Valor = Escreve(funcionario.subs_produtivade.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "Sub. Alimentação", Coef = "", Valor = Escreve(funcionario.subs_alimentacao.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "Sub. Férias", Coef = "", Valor = Escreve(funcionario.sub_ferias.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "Sub. Natal", Coef = "", Valor = Escreve(funcionario.sub_natal.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "Horas Extras", Coef = "", Valor = "" });
            ladoEsq.Add(new LinhaVencimento() { Abono = "50% Horas Extras", Coef = Escreve(funcionario.horasExtra50.ToString(), "0", ""), Valor = Escreve(funcionario.valorHorasExtra50.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "75% Horas Extra", Coef = Escreve(funcionario.horasExtra75.ToString(), "0", ""), Valor = Escreve(funcionario.valorHorasExtra75.ToString(), "0", "") });
            ladoEsq.Add(new LinhaVencimento() { Abono = "100% Horas Extra", Coef = Escreve(funcionario.horasExtra100.ToString(), "0", ""), Valor = Escreve(funcionario.valorHorasExtra100.ToString(), "0", "") });
            return ladoEsq;
        }

        private List<LinhaVencimento> ObterDescontos(hrp_facturacao_funcionario_detalhe funcionario)
        {
            List<LinhaVencimento> ladoDir = new List<LinhaVencimento>();
            ladoDir.Add(new LinhaVencimento() { Abono = "Faltas", Coef = "", Valor = "" });
            ladoDir.Add(new LinhaVencimento() { Abono = "Justificadas", Coef = Escreve(funcionario.faltas_justificadas.ToString(), "0", ""), Valor = Escreve(funcionario.valor_faltas_justificadas.ToString(), "0", "") });
            ladoDir.Add(new LinhaVencimento() { Abono = "Injustificadas", Coef = Escreve(funcionario.faltas_injustificadas.ToString(), "0", ""), Valor = Escreve(funcionario.valor_faltas_injustificadas.ToString(), "0", "") });
            ladoDir.Add(new LinhaVencimento() { Abono = "", Coef = "", Valor = "" });
            ladoDir.Add(new LinhaVencimento() { Abono = "Outros Descontos", Coef = "", Valor = "" });
            ladoDir.Add(new LinhaVencimento() { Abono = "IRT", Coef = "", Valor = funcionario.total_desconto_irt.ToString() });
            ladoDir.Add(new LinhaVencimento() { Abono = "Seg. Social", Coef = "", Valor = funcionario.valor_desconto_ss.ToString() });
            ladoDir.Add(new LinhaVencimento() { Abono = "Sindicato", Coef = "", Valor = Escreve(funcionario.valor_sindicato.ToString(), "0", "") });
            ladoDir.Add(new LinhaVencimento() { Abono = "Adiantamentos", Coef = "", Valor = Escreve(funcionario.valor_adiantamento.ToString(), "0", "") });
            return ladoDir;
        }

        private void ImprimirListaVencimentos()
        {
            int vencId = 0;
            Int32.TryParse(Request.QueryString["id"], out vencId);
            hrp_empresas empresa = (hrp_empresas)Session["EmpresaActual"];
            var ActualListaImpressao = Facturacao_BLL.Instance.ListaFacturacaoDetalhada(vencId);
            if (Request.QueryString["reciboid"] != null)
            {
                string[] ids = Request.QueryString["reciboid"].ToString().Split('_');
                HashSet<int> validosIds = new HashSet<int>();
                foreach (string receivedId in ids)
                {
                    int id = 0;
                    Int32.TryParse(receivedId, out id);
                    if (id != 0)
                        validosIds.Add(id);
                }
                ActualListaImpressao = ActualListaImpressao.Where(f => validosIds.Contains(f.id)).ToList();
            }
            if (ActualListaImpressao.Count > 0)
            {
                PdfDocument document = new PdfDocument();
                hrp_facturacao_funcionario_detalhe primeiroFuncionario = ActualListaImpressao[0];
                string tituloDocumento = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(Convert.ToInt32(primeiroFuncionario.mes_referente)) + " " + primeiroFuncionario.ano_referente;
                document.Info.Title = ActualListaImpressao.Count == 1 ? tituloDocumento : tituloDocumento + " " + primeiroFuncionario.nome_func;
                document.Info.Author = autor;
                document.Info.Subject = assunto;
                foreach (hrp_facturacao_funcionario_detalhe funcionario in ActualListaImpressao)
                {
                    List<LinhaVencimento> ladoEsq = ObterAbonos(funcionario);
                    List<LinhaVencimento> ladoDir = ObterDescontos(funcionario);
                    string dataRecibo = new CultureInfo("pt-PT").DateTimeFormat.GetMonthName(Convert.ToInt32(funcionario.mes_referente)) + " " + funcionario.ano_referente;
                    PdfPage page = document.AddPage();
                    page.Width = XUnit.FromMillimeter(297); // A4
                    page.Height = XUnit.FromMillimeter(210);
                    GerarVencimento(ref page, ladoEsq, ladoDir, funcionario.nome_func, funcionario.numero_funcionario.ToString(),
                    funcionario.departamento, funcionario.cargo, funcionario.nif.ToString(), funcionario.nif_empresa, funcionario.nome_empresa, Server.MapPath(empresa.logo),
                    funcionario.total_abonos.ToString(), funcionario.total_descontos.ToString(), funcionario.totalReceberLiquido.ToString(), dataRecibo);
                }
                // Send PDF to browser
                MemoryStream stream = new MemoryStream();
                document.Save(stream, false);
                Response.Clear();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-length", stream.Length.ToString());
                Response.BinaryWrite(stream.ToArray());
                Response.Flush();
                stream.Close();
                Response.End();
            }
        }

        private void GerarVencimento(ref PdfPage page, List<LinhaVencimento> ladoEsq, List<LinhaVencimento> ladoDir, string nomeFuncionario,
            string numFuncionario, string depFunc, string cargoFunc, string nifFunc, string nifEmpresa, string nomeEmpresa,
            string logoEmpresaUrl, string abonos, string descontos, string liquidoReceber, string mesRecibo)
        {
            XGraphics gfx = XGraphics.FromPdfPage(page);
            //Rectangle rectangle = new Rectangle(1,1,840,590);
            XRect rectOriginal = new XRect(10, 15, 400, 560);
            gfx.DrawRectangle(XPens.Black, rectOriginal);
            XRect rectDuplicado = new XRect(430, 15, 400, 560);
            gfx.DrawRectangle(XPens.Black, rectDuplicado);

            /***************ORIGINAL***************************************************/

            //logotipo
            XImage logotipo = XImage.FromFile(logoEmpresaUrl);
            XRect rectLogotipo = new XRect(20, 20, 180, 90);
            gfx.DrawImage(logotipo, rectLogotipo);
            //rect do recibo + data
            int posTitulo = 220;
            string tipoRecibo = "Original";
            int posTipoRecibo = 220;
            int posMesRecibo = 220;
            gfx.DrawString(TituloLabel, new XFont(font, 14, XFontStyle.Bold), XBrushes.Black, posTitulo, 35);
            gfx.DrawString(tipoRecibo, new XFont(font, 10), XBrushes.Black, posTipoRecibo, 48);
            gfx.DrawString(mesRecibo, new XFont(font, 12, XFontStyle.Bold), XBrushes.Black, posMesRecibo, 70);
            //dados Empresa
            gfx.DrawString(NomeEmpresaLabel, new XFont(font, 8, XFontStyle.Bold), XBrushes.Black, 220, 90);

            int posNomeEmpresa = 220 + 8 + NomeEmpresaLabel.Length * 4;
            int posNifEmpresa = 220 + 8 + NumContribuinteLabel.Length * 4;
            string primeiroNome = nomeEmpresa;
            string segundoNome = "";
            if (nomeEmpresa.Length > 32)
            {
                string primeiraMetade = nomeEmpresa.Substring(0, 32);
                int ultimoEspaco = primeiraMetade.LastIndexOf(' ');
                primeiroNome = nomeEmpresa.Substring(0, ultimoEspaco);
                segundoNome = nomeEmpresa.Substring(ultimoEspaco + 1, Math.Min(32, nomeEmpresa.Length - ultimoEspaco - 1));
            }
            if (string.IsNullOrEmpty(segundoNome))
            {
                gfx.DrawString(NumContribuinteLabel, new XFont(font, 8, XFontStyle.Bold), XBrushes.Black, 220, 100);
                gfx.DrawString(nomeEmpresa, new XFont(font, 8), XBrushes.Black, posNomeEmpresa, 90);
                gfx.DrawString(nifEmpresa, new XFont(font, 8), XBrushes.Black, posNifEmpresa, 100);
            }
            else
            {
                gfx.DrawString(NumContribuinteLabel, new XFont(font, 8, XFontStyle.Bold), XBrushes.Black, 220, 110);
                gfx.DrawString(primeiroNome, new XFont(font, 8), XBrushes.Black, posNomeEmpresa, 90);
                gfx.DrawString(segundoNome, new XFont(font, 8), XBrushes.Black, posNomeEmpresa, 100);
                gfx.DrawString(nifEmpresa, new XFont(font, 8), XBrushes.Black, posNifEmpresa, 110);
            }

            //dados funcionario
            XRect rectNomeFunc = new XRect(20, 120, 384, 20);
            XRect rectNumeroFunc = new XRect(20, 140, 114, 20);
            XRect rectNifFunc = new XRect(20, 160, 114, 20);
            XRect rectDepartFunc = new XRect(134, 140, 270, 20);
            XRect rectCargoFunc = new XRect(134, 160, 270, 20);
            gfx.DrawRectangle(XPens.Black, rectNomeFunc);
            gfx.DrawRectangle(XPens.Black, rectNumeroFunc);
            gfx.DrawRectangle(XPens.Black, rectDepartFunc);
            gfx.DrawRectangle(XPens.Black, rectNifFunc);
            gfx.DrawRectangle(XPens.Black, rectCargoFunc);

            XRect rectNomeLabel = new XRect(20, 120, 50, 20);
            XRect rectNumeroLabel = new XRect(20, 140, 50, 20);
            XRect rectNifLabel = new XRect(20, 160, 50, 20);
            XRect rectDepartLabel = new XRect(134, 140, 60, 20);
            XRect rectCargoLabel = new XRect(134, 160, 60, 20);
            gfx.DrawRectangle(XPens.Black, rectNomeLabel);
            gfx.DrawRectangle(XPens.Black, rectNumeroLabel);
            gfx.DrawRectangle(XPens.Black, rectDepartLabel);
            gfx.DrawRectangle(XPens.Black, rectNifLabel);
            gfx.DrawRectangle(XPens.Black, rectCargoLabel);

            gfx.DrawString(NomeFuncLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 22, 135);
            gfx.DrawString(NumeroFuncLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 22, 155);
            gfx.DrawString(NumContribuinteFuncLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 22, 175);
            gfx.DrawString(DepartamentoLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 136, 155);
            gfx.DrawString(CargoLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 136, 175);
            //insercao dados funcionario
            gfx.DrawString(nomeFuncionario, new XFont(font, 10), XBrushes.Black, 72, 135);
            gfx.DrawString(numFuncionario, new XFont(font, 10), XBrushes.Black, 72, 155);
            gfx.DrawString(nifFunc, new XFont(font, 10), XBrushes.Black, 72, 175);
            gfx.DrawString(depFunc, new XFont(font, 10), XBrushes.Black, 196, 155);
            gfx.DrawString(cargoFunc, new XFont(font, 10), XBrushes.Black, 196, 175);
            //cabecalho tabela
            XRect rectHeader = new XRect(20, 190, 384, 20);
            XRect rectAbono = new XRect(20, 190, 102, 20);
            XRect rectCoefEsq = new XRect(122, 190, 35, 20);
            XRect rectValorEsq = new XRect(157, 190, 55, 20);
            XRect rectDesconto = new XRect(212, 190, 102, 20);
            XRect rectCoefDir = new XRect(314, 190, 35, 20);
            XRect rectValorDir = new XRect(349, 190, 55, 20);
            gfx.DrawRectangle(XPens.Black, rectHeader);
            gfx.DrawRectangle(XPens.Black, rectAbono);
            gfx.DrawRectangle(XPens.Black, rectCoefEsq);
            gfx.DrawRectangle(XPens.Black, rectValorEsq);
            gfx.DrawRectangle(XPens.Black, rectDesconto);
            gfx.DrawRectangle(XPens.Black, rectCoefDir);
            gfx.DrawRectangle(XPens.Black, rectValorDir);
            gfx.DrawString(AbonoLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 55, 205);
            gfx.DrawString(CoefLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 124, 205);
            gfx.DrawString(ValorLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 174, 205);
            gfx.DrawString(DescontoLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 237, 205);
            gfx.DrawString(CoefLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 317, 205);
            gfx.DrawString(ValorLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 366, 205);

            //LinhasEsq
            int posHInicial = 210;
            for (int i = 0; i < ladoEsq.Count; i++)
            {
                XRect linhaAbono = new XRect(20, (i * 10) + posHInicial, 102, 10);
                XRect linhaCoefEsq = new XRect(122, (i * 10) + posHInicial, 35, 10);
                XRect linhaValorEsq = new XRect(157, (i * 10) + posHInicial, 55, 10);
                gfx.DrawRectangle(XPens.Black, linhaAbono);
                gfx.DrawRectangle(XPens.Black, linhaCoefEsq);
                gfx.DrawRectangle(XPens.Black, linhaValorEsq);
                gfx.DrawString(ladoEsq[i].Abono, new XFont(font, 8), XBrushes.Black, 25, (i * 10) + posHInicial + 8);
                gfx.DrawString(ladoEsq[i].Coef, new XFont(font, 8), XBrushes.Black, linhaCoefEsq.Center.X - 5, (i * 10) + posHInicial + 8);
                int posX = 212 - 2 - (ladoEsq[i].Valor.Length * 5);
                gfx.DrawString(ladoEsq[i].Valor, new XFont(font, 8), XBrushes.Black, posX, (i * 10) + posHInicial + 8);
            }
            for (int i = 0; i < ladoDir.Count; i++)
            {
                XRect linhaAbono = new XRect(212, (i * 10) + posHInicial, 102, 10);
                XRect linhaCoefDir = new XRect(314, (i * 10) + posHInicial, 35, 10);
                XRect linhaValorDir = new XRect(349, (i * 10) + posHInicial, 55, 10);
                gfx.DrawRectangle(XPens.Black, linhaAbono);
                gfx.DrawRectangle(XPens.Black, linhaCoefDir);
                gfx.DrawRectangle(XPens.Black, linhaValorDir);
                gfx.DrawString(ladoDir[i].Abono, new XFont(font, 8), XBrushes.Black, 217, (i * 10) + posHInicial + 8);
                gfx.DrawString(ladoDir[i].Coef, new XFont(font, 8), XBrushes.Black, linhaCoefDir.Center.X - 5, (i * 10) + posHInicial + 8);
                int posX = 402 - 2 - (ladoDir[i].Valor.Length * 5);
                gfx.DrawString(ladoDir[i].Valor, new XFont(font, 8), XBrushes.Black, posX, (i * 10) + posHInicial + 8);
            }
            XRect rectTabela = new XRect(20, posHInicial, 384, ladoEsq.Count * 10);
            gfx.DrawRectangle(XPens.Black, rectTabela);

            //linha resumo
            int alturaActual = ladoEsq.Count * 10 + posHInicial + 10;
            XRect rectResumoHeader = new XRect(20, alturaActual, 384, 20);
            XRect rectResumoValores = new XRect(20, alturaActual + 20, 384, 20);
            gfx.DrawRectangle(XPens.Black, rectResumoHeader);
            gfx.DrawRectangle(XPens.Black, rectResumoValores);
            gfx.DrawLine(XPens.Black, new XPoint(157, alturaActual), new XPoint(157, alturaActual + 40));
            gfx.DrawLine(XPens.Black, new XPoint(272, alturaActual), new XPoint(272, alturaActual + 40));
            gfx.DrawString(TotalAbonosLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 65, alturaActual + 15);
            gfx.DrawString(TotalDescontosLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 185, alturaActual + 15);
            gfx.DrawString(LiquidoReceberLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 287, alturaActual + 15);
            int posXAbonos = 157 - 13 - (abonos.Length * 6);
            gfx.DrawString(abonos, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXAbonos, alturaActual + 35);
            int posXDescontos = 272 - 10 - (descontos.Length * 6);
            gfx.DrawString(descontos, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDescontos, alturaActual + 35);
            int posXLiquidoReceber = 402 - 12 - (liquidoReceber.Length * 6);
            gfx.DrawString(liquidoReceber, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXLiquidoReceber, alturaActual + 35);

            //assinatura
            alturaActual = ladoEsq.Count * 10 + posHInicial + 10 + 50;
            XRect rectAssinData = new XRect(20, alturaActual, 384, 20);
            XRect rectAssin = new XRect(20, alturaActual + 20, 384, 40);
            XRect rectAssinMsg = new XRect(20, alturaActual + 60, 384, 20);
            gfx.DrawRectangle(XPens.Black, rectAssinData);
            gfx.DrawRectangle(XPens.Black, rectAssin);
            gfx.DrawRectangle(XPens.Black, rectAssinMsg);
            gfx.DrawLine(XPens.Black, new XPoint(122, alturaActual), new XPoint(122, alturaActual + 60));
            gfx.DrawString(DataLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 25, alturaActual + 15);
            gfx.DrawString(AssinaturaLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, 25, alturaActual + 42);
            gfx.DrawString(DeclaracaoLabel, new XFont(font, 7, XFontStyle.Bold), XBrushes.Black, 25, alturaActual + 75);



            /***************DUPLICADO***************************************************/
            int posXDuplicadoInicial = 420;
            //logotipo
            rectLogotipo = new XRect(posXDuplicadoInicial + 20, 20, 180, 90);
            gfx.DrawImage(logotipo, rectLogotipo);
            //rect do recibo + data
            posTitulo = posXDuplicadoInicial + 220;
            tipoRecibo = "Duplicado";
            posTipoRecibo = posXDuplicadoInicial + 220;
            posMesRecibo = posXDuplicadoInicial + 220;
            gfx.DrawString(TituloLabel, new XFont(font, 14, XFontStyle.Bold), XBrushes.Black, posTitulo, 35);
            gfx.DrawString(tipoRecibo, new XFont(font, 10), XBrushes.Black, posTipoRecibo, 48);
            gfx.DrawString(mesRecibo, new XFont(font, 12, XFontStyle.Bold), XBrushes.Black, posMesRecibo, 70);
            //dados Empresa
            gfx.DrawString(NomeEmpresaLabel, new XFont(font, 8, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 220, 90);

            posNomeEmpresa = posXDuplicadoInicial + 220 + 8 + NomeEmpresaLabel.Length * 4;
            posNifEmpresa = posXDuplicadoInicial + 220 + 8 + NumContribuinteLabel.Length * 4;
            primeiroNome = nomeEmpresa;
            segundoNome = "";
            if (nomeEmpresa.Length > 32)
            {
                string primeiraMetade = nomeEmpresa.Substring(0, 32);
                int ultimoEspaco = primeiraMetade.LastIndexOf(' ');
                primeiroNome = nomeEmpresa.Substring(0, ultimoEspaco);
                segundoNome = nomeEmpresa.Substring(ultimoEspaco + 1, Math.Min(32, nomeEmpresa.Length - ultimoEspaco - 1));
            }
            if (string.IsNullOrEmpty(segundoNome))
            {
                gfx.DrawString(NumContribuinteLabel, new XFont(font, 8, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 220, 100);
                gfx.DrawString(nomeEmpresa, new XFont(font, 8), XBrushes.Black, posNomeEmpresa, 90);
                gfx.DrawString(nifEmpresa, new XFont(font, 8), XBrushes.Black, posNifEmpresa, 100);
            }
            else
            {
                gfx.DrawString(NumContribuinteLabel, new XFont(font, 8, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 220, 110);
                gfx.DrawString(primeiroNome, new XFont(font, 8), XBrushes.Black, posNomeEmpresa, 90);
                gfx.DrawString(segundoNome, new XFont(font, 8), XBrushes.Black, posNomeEmpresa, 100);
                gfx.DrawString(nifEmpresa, new XFont(font, 8), XBrushes.Black, posNifEmpresa, 110);
            }

            //dados funcionario
            rectNomeFunc = new XRect(posXDuplicadoInicial + 20, 120, 384, 20);
            rectNumeroFunc = new XRect(posXDuplicadoInicial + 20, 140, 114, 20);
            rectNifFunc = new XRect(posXDuplicadoInicial + 20, 160, 114, 20);
            rectDepartFunc = new XRect(posXDuplicadoInicial + 134, 140, 270, 20);
            rectCargoFunc = new XRect(posXDuplicadoInicial + 134, 160, 270, 20);
            gfx.DrawRectangle(XPens.Black, rectNomeFunc);
            gfx.DrawRectangle(XPens.Black, rectNumeroFunc);
            gfx.DrawRectangle(XPens.Black, rectDepartFunc);
            gfx.DrawRectangle(XPens.Black, rectNifFunc);
            gfx.DrawRectangle(XPens.Black, rectCargoFunc);

            rectNomeLabel = new XRect(posXDuplicadoInicial + 20, 120, 50, 20);
            rectNumeroLabel = new XRect(posXDuplicadoInicial + 20, 140, 50, 20);
            rectNifLabel = new XRect(posXDuplicadoInicial + 20, 160, 50, 20);
            rectDepartLabel = new XRect(posXDuplicadoInicial + 134, 140, 60, 20);
            rectCargoLabel = new XRect(posXDuplicadoInicial + 134, 160, 60, 20);
            gfx.DrawRectangle(XPens.Black, rectNomeLabel);
            gfx.DrawRectangle(XPens.Black, rectNumeroLabel);
            gfx.DrawRectangle(XPens.Black, rectDepartLabel);
            gfx.DrawRectangle(XPens.Black, rectNifLabel);
            gfx.DrawRectangle(XPens.Black, rectCargoLabel);

            gfx.DrawString(NomeFuncLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 22, 135);
            gfx.DrawString(NumeroFuncLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 22, 155);
            gfx.DrawString(NumContribuinteFuncLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 22, 175);
            gfx.DrawString(DepartamentoLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 136, 155);
            gfx.DrawString(CargoLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 136, 175);
            //insercao dados funcionario
            gfx.DrawString(nomeFuncionario, new XFont(font, 10), XBrushes.Black, posXDuplicadoInicial + 72, 135);
            gfx.DrawString(numFuncionario, new XFont(font, 10), XBrushes.Black, posXDuplicadoInicial + 72, 155);
            gfx.DrawString(nifFunc, new XFont(font, 10), XBrushes.Black, posXDuplicadoInicial + 72, 175);
            gfx.DrawString(depFunc, new XFont(font, 10), XBrushes.Black, posXDuplicadoInicial + 196, 155);
            gfx.DrawString(cargoFunc, new XFont(font, 10), XBrushes.Black, posXDuplicadoInicial + 196, 175);
            //cabecalho tabela
            rectHeader = new XRect(posXDuplicadoInicial + 20, 190, 384, 20);
            rectAbono = new XRect(posXDuplicadoInicial + 20, 190, 102, 20);
            rectCoefEsq = new XRect(posXDuplicadoInicial + 122, 190, 35, 20);
            rectValorEsq = new XRect(posXDuplicadoInicial + 157, 190, 55, 20);
            rectDesconto = new XRect(posXDuplicadoInicial + 212, 190, 102, 20);
            rectCoefDir = new XRect(posXDuplicadoInicial + 314, 190, 35, 20);
            rectValorDir = new XRect(posXDuplicadoInicial + 349, 190, 55, 20);
            gfx.DrawRectangle(XPens.Black, rectHeader);
            gfx.DrawRectangle(XPens.Black, rectAbono);
            gfx.DrawRectangle(XPens.Black, rectCoefEsq);
            gfx.DrawRectangle(XPens.Black, rectValorEsq);
            gfx.DrawRectangle(XPens.Black, rectDesconto);
            gfx.DrawRectangle(XPens.Black, rectCoefDir);
            gfx.DrawRectangle(XPens.Black, rectValorDir);
            gfx.DrawString(AbonoLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 55, 205);
            gfx.DrawString(CoefLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 124, 205);
            gfx.DrawString(ValorLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 174, 205);
            gfx.DrawString(DescontoLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 237, 205);
            gfx.DrawString(CoefLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 317, 205);
            gfx.DrawString(ValorLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 366, 205);

            //LinhasEsq
            posHInicial = 210;
            for (int i = 0; i < ladoEsq.Count; i++)
            {
                XRect linhaAbono = new XRect(posXDuplicadoInicial + 20, (i * 10) + posHInicial, 102, 10);
                XRect linhaCoefEsq = new XRect(posXDuplicadoInicial + 122, (i * 10) + posHInicial, 35, 10);
                XRect linhaValorEsq = new XRect(posXDuplicadoInicial + 157, (i * 10) + posHInicial, 55, 10);
                gfx.DrawRectangle(XPens.Black, linhaAbono);
                gfx.DrawRectangle(XPens.Black, linhaCoefEsq);
                gfx.DrawRectangle(XPens.Black, linhaValorEsq);
                gfx.DrawString(ladoEsq[i].Abono, new XFont(font, 8), XBrushes.Black, posXDuplicadoInicial + 25, (i * 10) + posHInicial + 8);
                gfx.DrawString(ladoEsq[i].Coef, new XFont(font, 8), XBrushes.Black, linhaCoefEsq.Center.X - 5, (i * 10) + posHInicial + 8);
                int posX = posXDuplicadoInicial + 212 - 2 - (ladoEsq[i].Valor.Length * 5);
                gfx.DrawString(ladoEsq[i].Valor, new XFont(font, 8), XBrushes.Black, posX, (i * 10) + posHInicial + 8);
            }
            for (int i = 0; i < ladoDir.Count; i++)
            {
                XRect linhaAbono = new XRect(posXDuplicadoInicial + 212, (i * 10) + posHInicial, 102, 10);
                XRect linhaCoefDir = new XRect(posXDuplicadoInicial + 314, (i * 10) + posHInicial, 35, 10);
                XRect linhaValorDir = new XRect(posXDuplicadoInicial + 349, (i * 10) + posHInicial, 55, 10);
                gfx.DrawRectangle(XPens.Black, linhaAbono);
                gfx.DrawRectangle(XPens.Black, linhaCoefDir);
                gfx.DrawRectangle(XPens.Black, linhaValorDir);
                gfx.DrawString(ladoDir[i].Abono, new XFont(font, 8), XBrushes.Black, posXDuplicadoInicial + 217, (i * 10) + posHInicial + 8);
                gfx.DrawString(ladoDir[i].Coef, new XFont(font, 8), XBrushes.Black, linhaCoefDir.Center.X - 5, (i * 10) + posHInicial + 8);
                int posX = posXDuplicadoInicial + 402 - 2 - (ladoDir[i].Valor.Length * 5);
                gfx.DrawString(ladoDir[i].Valor, new XFont(font, 8), XBrushes.Black, posX, (i * 10) + posHInicial + 8);
            }
            rectTabela = new XRect(posXDuplicadoInicial + 20, posHInicial, 384, ladoEsq.Count * 10);
            gfx.DrawRectangle(XPens.Black, rectTabela);

            //linha resumo
            alturaActual = ladoEsq.Count * 10 + posHInicial + 10;
            rectResumoHeader = new XRect(posXDuplicadoInicial + 20, alturaActual, 384, 20);
            rectResumoValores = new XRect(posXDuplicadoInicial + 20, alturaActual + 20, 384, 20);
            gfx.DrawRectangle(XPens.Black, rectResumoHeader);
            gfx.DrawRectangle(XPens.Black, rectResumoValores);
            gfx.DrawLine(XPens.Black, new XPoint(posXDuplicadoInicial + 157, alturaActual), new XPoint(posXDuplicadoInicial + 157, alturaActual + 40));
            gfx.DrawLine(XPens.Black, new XPoint(posXDuplicadoInicial + 272, alturaActual), new XPoint(posXDuplicadoInicial + 272, alturaActual + 40));
            gfx.DrawString(TotalAbonosLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 65, alturaActual + 15);
            gfx.DrawString(TotalDescontosLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 185, alturaActual + 15);
            gfx.DrawString(LiquidoReceberLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 287, alturaActual + 15);
            posXAbonos = posXDuplicadoInicial + 157 - 13 - (abonos.Length * 6);
            gfx.DrawString(abonos, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXAbonos, alturaActual + 35);
            posXDescontos = posXDuplicadoInicial + 272 - 10 - (descontos.Length * 6);
            gfx.DrawString(descontos, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDescontos, alturaActual + 35);
            posXLiquidoReceber = posXDuplicadoInicial + 402 - 12 - (liquidoReceber.Length * 6);
            gfx.DrawString(liquidoReceber, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXLiquidoReceber, alturaActual + 35);

            //assinatura
            alturaActual = ladoEsq.Count * 10 + posHInicial + 10 + 50;
            rectAssinData = new XRect(posXDuplicadoInicial + 20, alturaActual, 384, 20);
            rectAssin = new XRect(posXDuplicadoInicial + 20, alturaActual + 20, 384, 40);
            rectAssinMsg = new XRect(posXDuplicadoInicial + 20, alturaActual + 60, 384, 20);
            gfx.DrawRectangle(XPens.Black, rectAssinData);
            gfx.DrawRectangle(XPens.Black, rectAssin);
            gfx.DrawRectangle(XPens.Black, rectAssinMsg);
            gfx.DrawLine(XPens.Black, new XPoint(posXDuplicadoInicial + 122, alturaActual), new XPoint(posXDuplicadoInicial + 122, alturaActual + 60));
            gfx.DrawString(DataLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 25, alturaActual + 15);
            gfx.DrawString(AssinaturaLabel, new XFont(font, 10, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 25, alturaActual + 42);
            gfx.DrawString(DeclaracaoLabel, new XFont(font, 7, XFontStyle.Bold), XBrushes.Black, posXDuplicadoInicial + 25, alturaActual + 75);

        }
    }
}